﻿@using Newbe.BookmarkManager.Services
@using Newbe.BookmarkManager.Services.EventHubs
@using Autofac
@if (_userOptions is {
    CloudBkFeature : {
        Enabled:true,
        }
    })
{
    switch (_userOptions.CloudBkFeature.CloudBkProviderType)
    {
        case CloudBkProviderType.NewbeApi:
            break;
        case CloudBkProviderType.GoogleDrive:
            <GoogleLoginButton/>
            break;
        case CloudBkProviderType.OneDrive:
            <MicrosoftLogin/>
            break;
        default:
            throw new ArgumentOutOfRangeException();
    }
}

@code {

    [Inject]
    public IUserOptionsService UserOptionsService { get; set; }

    [Inject]
    public IAfEventHub AfEventHub { get; set; }

    [Inject]
    public IClock Clock { get; set; }

    private UserOptions? _userOptions;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        _userOptions = await UserOptionsService.GetOptionsAsync();
        StateHasChanged();
        AfEventHub.RegisterHandler<UserOptionSaveEvent>(HandleUserOptionSaveEvent);
        await AfEventHub.EnsureStartAsync();
    }

    private Task HandleUserOptionSaveEvent(UserOptionSaveEvent evt)
    {
        return InvokeAsync(() =>
        {
            _userOptions = evt.UserOptions;
            StateHasChanged();
        });
    }

}